package com.dream.service.recommender.toolkits.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class RecConfigUtil {

    static Properties properties;

    private static Map<String,String> rec_info  = new HashMap<String, String>();

    static {
    	InputStream inputStream = null;
        try {
        	 inputStream  = RecConfigUtil.class.getClassLoader().getResourceAsStream("recommend.properties");
        	properties = new Properties();
            properties.load(inputStream);
            loadRecomInfo();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void loadRecomInfo(){
        if(properties ==null){
            return ;
        }
        Enumeration enumeration =  properties.propertyNames();
        while(enumeration.hasMoreElements()){
            String key = enumeration.nextElement().toString();
            if(key.startsWith("rec_")){
                String _key = key.replaceAll("rec_","");
                rec_info.put(_key, properties.getProperty(key));
            }
        }
    }

    public static String getPro(String key){
    	return getPro(key, "");
    }

    public static String getPro(String key, String def_value){
    	String value = properties.getProperty(key);
    	if(value == null || value.trim().equals("")){
    		value = def_value;
    	}
    	return value;
    }

    public static Map<String, String> getCacheStrategyInfo() {
        if(properties == null){
            return Collections.EMPTY_MAP;
        }
        Map<String, String> cacheStrategyMap = new HashMap<String, String>();
        Enumeration enumeration =  properties.propertyNames();
        while(enumeration.hasMoreElements()){
            String key = enumeration.nextElement().toString();
            if(key.startsWith("cache_strategy_")){
                String _key = key.replaceAll("cache_strategy_","");
                cacheStrategyMap.put(_key, properties.getProperty(key));
            }
        }
        return cacheStrategyMap;
    }

    public static Map<String,String> getRecInfo(){
        return rec_info;
    }
}
